package com.qingfeng.client.service;

import com.qingfeng.client.VO.AfterSellApplyVo;
import com.qingfeng.client.bean.AfterSell;
import com.qingfeng.client.bean.OrderGoods;
import com.qingfeng.client.bean.OrderStore;
import com.qingfeng.client.bean.Trade;
import com.qingfeng.client.dao.AfterSellMapper;
import com.qingfeng.client.dao.OrderGoodsMapper;
import com.qingfeng.client.dao.OrderStoreMapper;
import com.qingfeng.client.dao.TradeMapper;
import com.qingfeng.client.util.MakeOrderNum;
import com.qingfeng.client.util.RefundEnum;
import com.qingfeng.client.util.TradeEnum;
import com.qingfeng.exception.LsException;
import com.qingfeng.util.CodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/3/11 3:32 下午
 */
@Service
public class AfterSellService {
    @Resource
    private OrderGoodsMapper orderGoodsMapper;
    @Resource
    private OrderStoreMapper orderStoreMapper;
    @Resource
    private AfterSellMapper afterSellMapper;
    @Resource
    private TradeMapper tradeMapper;

    /**
     * 申请退款
     * @param afterSellApplyVo
     * @return
     */
    @Transactional
    public void apply(AfterSellApplyVo afterSellApplyVo,long buyerId){
        OrderGoods orderGoods = orderGoodsMapper.selectById(afterSellApplyVo.getOrderGoodsId());
        if (orderGoods.getStat().equals(TradeEnum.TRADE_CLOSED.getCode())){
            throw new LsException(CodeEnum.ERROR,"该笔订单已关闭，无法退款");
        }
        if (orderGoods.getStat().equals(TradeEnum.TRADE_WAIT_PAY.getCode())){
            throw new LsException(CodeEnum.ERROR,"该笔订单未付款");
        }
        Trade trade = tradeMapper.selectById(orderGoods.getTradeId());
        OrderStore orderStore = orderStoreMapper.selectById(orderGoods.getOrderStoreId());
        List<OrderGoods> list = orderGoodsMapper.selectByOrderStoreId(orderGoods.getOrderStoreId());
        int refundNum=0;
        for(OrderGoods order:list){
            if (order.getStat().equals(TradeEnum.TRADE_REFUND_SUCCESS.getCode()) ||
                    order.getStat().equals(TradeEnum.TRADE_REFUND.getCode()) ||
                    order.getStat().equals(TradeEnum.TRADE_REFUND_GOODS.getCode())){
                refundNum++;
            }
        }
        if (refundNum==(list.size()-1)){
            if (afterSellApplyVo.getPrice()> (orderGoods.getTotalPrice()+orderStore.getFreight())){
                throw new LsException(CodeEnum.ERROR,"退款金额超过商品价格和运费总和");
            }
        }else {
            if (afterSellApplyVo.getPrice()> orderGoods.getTotalPrice()){
                throw new LsException(CodeEnum.ERROR,"退款金额超过商品价格");
            }
        }

        if (afterSellApplyVo.getPrice()>trade.getPayment()){
            throw new LsException(CodeEnum.ERROR,"退款金额超过总付款金额");
        }




        AfterSell afterSell = new AfterSell();
        List<AfterSell> afterSells = afterSellMapper.selectByBuyerIdAndOrderGoodsId(buyerId,orderGoods.getId());
        if (afterSells.size()>0){
            afterSell = afterSells.get(0);
            if (afterSell.getRefundStat().equals(RefundEnum.WAIT_BUYER_AGREE.getCode())&&
                    afterSell.getRefundStat().equals(RefundEnum.CLOSE.getCode())&&
                    afterSell.getRefundStat().equals(RefundEnum.SUCCESS.getCode())){
                throw new LsException(CodeEnum.ERROR,"该笔退款已经完成，无法再次退款。请联系卖家");
            }
            afterSell.setUpdateTime((int) (System.currentTimeMillis()/1000));
            afterSell.setUpdateId(buyerId);
        }else {
            //退款方式
            afterSell.setRefundType(afterSellApplyVo.getRefundType());
            //设置申请人
            afterSell.setBuyerId(buyerId);
            afterSell.setCreateId(buyerId);
            //退款编码--无
            MakeOrderNum makeOrderNum = new MakeOrderNum();
            afterSell.setCode(makeOrderNum.makeOrderNum());
            afterSell.setOrderStoreCode(orderStore.getCode());
            afterSell.setOrderGoodsId(orderGoods.getId());
            afterSell.setGoodsId(orderGoods.getGoodsId());
            afterSell.setGoodsAttrs(orderGoods.getGoodsAttrs());
            afterSell.setGoodsName(orderGoods.getGoodsName());
            afterSell.setGoodsPic(orderGoods.getGoodsPic());
            afterSell.setStoreId(orderStore.getStoreId());
            afterSell.setCreateTime((int) (System.currentTimeMillis()/1000));
        }
        afterSell.setLogisticeCode(afterSellApplyVo.getLogisticsCode());
        afterSell.setLogisticeCompany(afterSellApplyVo.getLogisticeCompany());
        afterSell.setRefundType(afterSellApplyVo.getRefundType());
        afterSell.setRefundStat(RefundEnum.SELL_AGREE.getCode());
        afterSell.setBackMoney(afterSellApplyVo.getPrice());
        afterSell.setReason(afterSellApplyVo.getReason()+"");
        afterSell.setDescribes(afterSellApplyVo.getDescribe());
        afterSell.setPhone(afterSellApplyVo.getPhone());
        if (!StringUtils.isEmpty(afterSellApplyVo.getPic())) {
            String[] strPic = afterSellApplyVo.getPic().split(",");
            int count = 0;
            for (String picture : strPic) {
                if (!StringUtils.isEmpty(picture) && count == 0) {
                    afterSell.setPic1(picture);
                } else if (!StringUtils.isEmpty(picture) && count == 1) {
                    afterSell.setPic2(picture);
                } else if (!StringUtils.isEmpty(picture) && count == 2) {
                    afterSell.setPic3(picture);
                } else if (!StringUtils.isEmpty(picture) && count == 3) {
                    afterSell.setPic4(picture);
                } else if (!StringUtils.isEmpty(picture) && count == 4) {
                    afterSell.setPic5(picture);
                }
                count++;
            }
        }

        if (afterSell.getId()==null){
            afterSellMapper.insert(afterSell);
        }else {
            afterSellMapper.updateById(afterSell);
        }
        orderGoods.setStat(TradeEnum.TRADE_REFUND.getCode());
        orderGoodsMapper.updateById(orderGoods);
    }
}
